package controllers

import (
	"IrisService/models"
	"time"

	"github.com/jinzhu/gorm"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
)

type baseController struct {
	entry string
	db    *gorm.DB
	model models.BaseModel
	props []string
}

func GenBaseController(entry string, props []string) *baseController {
	return &baseController{
		entry: entry,
		db:    nil,
		model: nil,
		props: props}
}

func (c *baseController) Get() iris.Map {
	return c.GenIndex()
}

func (c *baseController) BeforeActivation(b mvc.BeforeActivation) {
	b.Handle("GET", "/_apis", "ShowApis")
}

func (c *baseController) ShowApis() iris.Map {
	return c.GenTopic("_apis", c.props)
}

func (c *baseController) Rise(data interface{}) iris.Map {
	return iris.Map{
		"code":   models.OK,
		"entry":  c.entry,
		"result": data}
}

func (c *baseController) Fail() iris.Map {
	return iris.Map{
		"code":  models.Failure,
		"entry": c.entry}
}

func (c *baseController) GenTopic(topic string,
	items interface{}) iris.Map {
	return iris.Map{
		"code":  models.OK,
		"entry": c.entry,
		topic:   items}
}

func (c *baseController) GenTopics(items interface{}) iris.Map {
	return iris.Map{
		"code":   models.OK,
		"entry":  c.entry,
		"topics": items}
}

func (c *baseController) GenDetails(items interface{}) iris.Map {
	return iris.Map{
		"code":    models.OK,
		"entry":   c.entry,
		"details": items}
}

func (c *baseController) GenIndex() iris.Map {
	return iris.Map{
		"code":  models.OK,
		"entry": c.entry,
		"time":  time.Now().Format("2006-01-02 15:04:05")}
}
